/**
 * 
 */
package com.ntv.service.news.impl;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ntv.common.NewsCategory;
import com.ntv.common.NewsType;
import com.ntv.domain.Content;
import com.ntv.domain.IContent;
import com.ntv.domain.INews;
import com.ntv.domain.INewsType;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.content.IContentDao;
import com.ntv.persistent.news.INewsDao;
import com.ntv.persistent.news.INewsTypeDao;
import com.ntv.service.DomainException;
import com.ntv.service.news.INewsAppService;

/**
 * @author Emperor
 * 
 */
public class NewsAppService implements INewsAppService {

	private static final Logger logger = Logger.getLogger(NewsAppService.class);
	private INewsDao newsDao;
	private IContentDao contentDao;
	private INewsTypeDao newsTypeDao;
	
	
	
	/**
	 * @param contentDao the contentDao to set
	 */
	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public INews create(INews argNews) throws DomainException {
		
		try {
			
			IContent content = new Content();
			
			if(argNews.getHasContent()){
				content.setContentSize(new Long(argNews.getImage().available()));
				content.setContent(argNews.getImage());
				content.setCaption(argNews.getCaption());
			}
			
			content.setCreatedBy(argNews.getCreatedBy());
			content.setCreatedOn(argNews.getCreatedOn()); 
			content.setUpdatedBy(argNews.getUpdatedBy());
			content.setUpdatedOn(argNews.getUpdatedOn());
			
			content = contentDao.create(content);
			argNews.setContentId(content.getContentId());
			
			return newsDao.createNews(argNews);
		} catch (PersistentException e) {
			throw new DomainException(e);
		}catch(IOException ioE){
			throw new DomainException(ioE);
		}
	}

	public void setNewsDao(INewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public INews findNewsById(Long id) throws DomainException {
		return (INews) newsDao.findById(id);
	}

	public List<INews> findAllNews() throws DomainException {
		try {
			return newsDao.getNews();
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getNews() throws DomainException {
		try {
			return newsDao.getNews();
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getLaststNewsList() throws DomainException {
		try {
			return newsDao.getLaststNewsList();
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getNataionNewsList() throws DomainException {
		try {
			List<INews> newsList = newsDao.getNewsByNewsType(NewsType.NATIONAL
					.getNewsType());
			logger.info("New List Size + " + newsList.size());
			return newsList;
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getInternationalNataionNewsList() throws DomainException {
		try {
			List<INews> newsList = newsDao
					.getNewsByNewsType(NewsType.INTERNATIONAL.getNewsType());
			logger.info("New List Size + " + newsList.size());
			return newsList;
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getCurrentDayNews() throws DomainException {
		try {
			return newsDao.getCurrentDayNews();
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public List<INews> getMostReadNewsList() throws DomainException {
		try {
			return newsDao.getMostReadNews();
		} catch (PersistentException e) {
			throw new DomainException(e);
		}

	}

	public INews getNewsByTypeAndNewsId(Long typeId, Long newsId)
			throws DomainException {
		try {
			return newsDao.getNewsByTypeAndNewsId(typeId, newsId);
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public Map getHomePageNewsObject() throws DomainException {
		Map map = new HashMap();

		NewsCategory[] categories = NewsCategory.values();
		try {
			for (NewsCategory newsCategory : categories) {
				List<INews> newList;

				newList = newsDao.getNewsByNewsType(newsCategory.getId());
				logger.info("newsCategory "+ newsCategory.getLabel() + newList.size());
				map.put(newsCategory.name(), newList);
			}
		} catch (PersistentException e) {
			throw new DomainException("Unexpected Exception ");
		}
		return map;
	}
	
	public List<INews> getNewsListByTypeId(Long typeId) throws DomainException {
		
		try {
			List<INews> newsList = newsDao.getNewsByNewsType(typeId);
			logger.info("newsCategory "+ typeId + newsList.size());
			return newsList;
		} catch (PersistentException e) {
			throw new DomainException("Unexpected Exception ");
		}
	}

	/**
	 * @param newsTypeDao the newsTypeDao to set
	 */
	public void setNewsTypeDao(INewsTypeDao newsTypeDao) {
		this.newsTypeDao = newsTypeDao;
	}
	
	public List<INewsType> getNewsTypes()throws DomainException{
		try{
			return newsTypeDao.getNewsTypes();
		} catch (PersistentException e) {
			throw new DomainException("Unexpected Exception ");
		}
	}

}
